{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyGeM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tutorial 2: Free Form Deformation on a cylinder in iges file format"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial we will show the typical workflow. In particular we are going to parse the parameters file for the FFD, read an iges file of a cylinder, perform the FFD and write the results on a new iges file. We also provide the example of the `show` function which is not present in [Tutorial 1](https://github.com/mathLab/PyGeM/tutorials/tutorial-1-stl.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First of all we just import the required PyGeM classes and we read a parameters file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pygem import FFDParameters, FFD, IgesHandler\n",
    "\n",
    "params = FFDParameters()\n",
    "params.read_parameters(filename='../tests/test_datasets/parameters_test_ffd_iges.prm')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we have to load the iges file on which we will perform the FFD. Since it is an iges we use the `IgesHandler` class. The `parse` method extracts the control points (or poles) coordinate of the NURBS surfaces that represent each face of the iges geometry without touching the topology."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "iges_handler = IgesHandler()\n",
    "mesh_points = iges_handler.parse('../tests/test_datasets/test_pipe.iges')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now use the show method to visualize the iges geometry. It basically load the file and shows its geometry. It is different from the plot method since:\n",
    "- it shows directly the geometry and not its triangulation\n",
    "- it can not save the picture in a png file (it shows only)\n",
    "\n",
    "Here you can see the result of \n",
    "\n",
    "\n",
    "    iges_handler.show('../tests/test_datasets/test_pipe.iges')\n",
    "\n",
    "\n",
    "![](pictures/cylinder_orig.png)\n",
    "\n",
    "For some \"backend\" issues we can show only one shape for session. Be careful when you try to show many geometries in the same script.\n",
    "\n",
    "Finally we can actually perform the FFD and then we can write the modified iges file with the new control points (or poles)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "free_form = FFD(params, mesh_points)\n",
    "free_form.perform()\n",
    "new_mesh_points = free_form.modified_mesh_points\n",
    "\n",
    "iges_handler.write(new_mesh_points, 'test_pipe_mod.iges')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes the write method can fail. In fact, in write function we perform some operations like \n",
    "- linking edges into a single wire; \n",
    "- trimming the surface with the wire.\n",
    "\n",
    "Often, a CAD file has its own tolerance and some entities that seems to be, for instance, linked actually are not.\n",
    "The linking and trimming operations can be hard for the software to be performed, especially when the starting CAD has not been made for analysis but for design purposes.\n",
    "Thus, for non trivial geometries it could be necessary to increase the tolerance.\n",
    "For the cylinder this is not the case, but we can increse the tolerance as well and write a, indeed almost equal, result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "iges_handler.write(new_mesh_points, 'test_pipe_mod.iges', 1e-3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here we show the final shape:\n",
    "\n",
    "    iges_handler.show('test_pipe_mod.iges')\n",
    "\n",
    "![](pictures/cylinder_mod.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
